#!/bin/sh
#
# USB WD My Book Fuel Gauge control script
# Copyright (C) 2007 Jonathan Krishnanantham, AsGF2MX@gmail.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, version 2. 
#
# How it works:
# This script takes in the device path (/dev/sdX) and obtains the 
# serial number of the specified device and uses it along with the 
# supplied value to set the LEDs.
#

#Usage and help statements
usage="Usage: wdsetleds [-d disk] [-s value] [-hqv]" ;
help="Valid options: \n
-d [disk path] - this path must be of the form /dev/sdX \n
-h - prints this message \n
-q - quiet mode \n
-s [value] - value must be between 0 and 63 \n
-v - verbose mode" ;

# Verbose mode and quiet, default = off
quiet=false ;
verbose=false ;

# Error statements
e_devicenotfound="Error: Could not find specified device." ;
e_ledvalue="Error: Please specify an LED value between 0 and 63." ;
e_multidisk="Error: Multiple disks specified." ;
e_multiled="Error: Multiple LED values specified." ;
e_notmybook="Error: Specified device is not a valid My Book device." ;
e_specifyone="Specify only one value." ;
e_unknown="Error: Failed to set LEDs." ;
e_verbose="Run with '-v' option for details." ; 
e_vqopterr="Invalid option; cannot be quiet AND verbose." ;


# Option error code
E_OPTERROR=65	;

# If no arguments were provided,return usage and set exit code
if [ $# -eq 0 ] ; 
then
  echo $usage ;
  exit $E_OPTERROR ;
fi 

# Get all the options
while getopts "d:s:vqh" options ;
do
  case $options in
    d ) if [ -z $disk ] ;
        then
          disk=$OPTARG ;
        else
          echo $e_multidisk $e_specifyone ;
          exit $E_OPTERROR ;
        fi ;;
    s ) if [ -z $ledvalue ] ;
        then
          ledvalue=$OPTARG ;
        else
          echo $e_multiled $e_specifyone ;
          exit $E_OPTERROR ; 
        fi ;;
    v ) verbose=true	;;
    q ) quiet=true	;;
    h ) echo $usage 	;
	echo -e $help	;
	exit 0		;;
    [?] ) echo $usage	;
	  exit 1	;;
    * ) echo $usage     ;
	exit 1		;;
  esac
done

# Options cannot be verbose and quiet at the same time!
if [ $verbose = true ] && [ $quiet = true ] ;
  then
    echo $e_vqopterr ;
    echo $usage ;
    exit $E_OPTERROR ;
fi

# Check if the specified disk exists
if [ -n "${disk}" ] && [ -b $disk ] ;
then 
  # Check if the led value is valid
  if [ -n "${ledvalue}" ] && [ $ledvalue -ge 0 ] && [ $ledvalue -lt 64 ] ; 
  then
    # Get vendor name
    vendor=$(udevinfo -q env -n $disk|grep ID_VENDOR) ;
    vendor=$(expr substr $vendor 11 128) ;
    # Get product name
    product=$(udevinfo -q env -n $disk|grep ID_MODEL) ;
    product=$(expr substr $product 10 128) ;
    # Get serial number
    serial=$(udevinfo -q env -n $disk|grep ID_SERIAL) ;
    serial=$(expr substr $serial $(expr ${#vendor} + ${#product} + 13) 128) ;

    # Print the info if it is verbose
    if [ $verbose = true ];
    then
      echo "Details of specified disk $disk :"
      echo "Vendor  :" $vendor ;
      echo "Product :" $product;
      echo "Serial  :" $serial ;
    fi

    # Check if it is a WD external device
    if [ "$vendor" != "WD" ] || [ -z $(echo $product|grep External) ] ;
    then
      if [ $quiet = false ] ;
      then
	    echo $e_notmybook ;
      fi
      exit 1 ;
    fi

    # Now call the wdleds program with the serial and the ledvalue
    if [ $verbose = true ];
    then
      # Show the output if it's verbose
      wdleds $serial $ledvalue ;
    else
      # Redirect any output to dev/null
      wdleds $serial $ledvalue > /dev/null 2>&1 ;
    fi

    # Store the wdleds exit status
    wdledstatus=$? ;
 
    # Print e_unknown message if exit status is not 0
    if [ $wdledstatus -ne 0 ] && [ $quiet = false ] ;
    then
      # If it was not run in verbose mode, suggest it.
      if [ $verbose = false ] ;
      then
	    echo $e_unknown  $e_verbose ;
      else
	    echo $e_unknown ;
      fi	
    fi

    # Return the exit status
    exit $wdledstatus ;      
  else
    if [ $quiet = false ];
    then
      echo $e_ledvalue ;
      echo $usage ;
    fi
    exit 1 ;
  fi
else
  if [ $quiet = false ];
  then
    echo $e_devicenotfound ;
    echo $usage	;
  fi
  exit 1 ;
fi
